home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / STACK.H < prev    next >
C/C++ Source or Header  |  1992-09-23  |  9KB  |  211 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MJF 05/22/89 -- Initial design.
  13. // Updated: JCB 06/05/89 -- Implementation.
  14. // Updated: LGO 08/09/89 -- Inherit from Generic
  15. // Updated: MBN 08/20/89 -- Changed usage of template to reflect new syntax
  16. // Updated: MBN 08/23/89 -- Added conditional exception handling and base class
  17. // Updated: LGO 10/05/89 -- Efficiency improvements to popn
  18. // Updated: MBN 10/19/89 -- Added optional argument to set_compare method
  19. // Updated: MBN 11/01/89 -- Added constructor with user-provided storage param
  20. // Updated: LGO 12/07/89 -- re-write push and pushn, added grow method
  21. // Updated: LGO 12/07/89 -- Make compare_s default to NULL
  22. // Updated: MBN 02/22/90 -- Changed size arguments from long to unsigned long
  23. // Updated: MJF 05/31/90 -- Use memcpy in resize
  24. // Updated: MJF 05/31/90 -- Use "delete [] data"
  25. // Updated: MJF 06/30/90 -- Added base class name to constructor initializer
  26. // Updated: VDN 02/21/92 -- New lite version
  27. // Updated: JAM 08/19/92 -- removed DOS specifics, stdized #includes
  28. // Updated: JAM 08/19/92 -- modernized template syntax, remove macro hacks
  29. //                          non-template classes Cool_Stack=>CoolBase_Stack
  30. //
  31. // The Stack<Type>  class is publicly  derived  from  the base Stack  class and
  32. // implements a one  dimensional  vector of  a  user-specified  type.   This is
  33. // accomplished by using the parameterized type  capability of C++.   The stack
  34. // will grow dynamically as necessary with the  amount  of growth determined by
  35. // the  value of an  allocation  size slot.   Fixed   length stacks   are  also
  36. // supported by setting the value of the allocation size slot to zero.
  37. //
  38. // Each Stack<Type> object contains a private  data section that  has a slot to
  39. // hold the current size of the stack and a pointer to an allocated block large
  40. // enough to hold "size elements" of type "Type."   A slot to  thold the number
  41. // of elements currently on  the stack is also available.   The protected  data
  42. // section has   a pointer  to a  compare function  that  is used   in equality
  43. // operations. The default equality function used is the == operator.
  44. //
  45. // There  are four   constructors   for the   Stack<Type> class.   The    first
  46. // constructor takes  no arguments and creates   an  empty  Stack object of the
  47. // specified type.  The second constructor takes a required argument specifying
  48. // the initial  size  of the stack.  The  third takes a pointer   to a block of
  49. // user-defined storage    and  the number of  elements  the   stack  can hold.
  50. // Finally,  the   third constructor  takes a  single argument  consisting of a
  51. // reference to a Stack<Type> and duplicates its size and element values.
  52. //
  53. // Methods are provided to push and  pop items to  and from the  stack, examine
  54. // the   item on the   top of the stack  without  removing  it, determine if an
  55. // element is already  on the stack, report the  number of items in the  stack,
  56. // check the empty status, and clear all items from the stack.  The assignment,
  57. // output, and  equality operators are  overloaded. Finally, two methods to set
  58. // the allocation growth size and compare function are provided.
  59. //
  60.  
  61. #ifndef STACKH                    // If no definition for Stack
  62. #define STACKH                    // Define stack symbol
  63.  
  64. #ifndef BASE_STACKH                // If no definition for class
  65. #include <cool/Base_Stack.h>            // Include definition file
  66. #endif
  67.  
  68. #ifndef NEWH
  69. #include <new.h>                // include the new header file
  70. #define NEWH
  71. #endif
  72.  
  73. template <class Type>
  74. class CoolStack : public CoolBase_Stack {
  75. public:
  76.   typedef Boolean (*Compare) (const Type&, const Type&);
  77.  
  78.   CoolStack();                // Simple constructor
  79.   CoolStack(unsigned long);        // CoolStack of initial size
  80.   CoolStack(void*, unsigned long);    // CoolStack with static storage
  81.   CoolStack(const CoolStack<Type>&);    // Copy constructor
  82.   ~CoolStack();                // Destructor
  83.  
  84.   Boolean push (const Type&);            // Push item on top of stack
  85.   Boolean pushn (const Type&, long);        // Push n items w/initial value
  86.  
  87.   inline Type& pop ();                // Pop top item off stack
  88.   Type& popn (long);                // Remove n items, return nth
  89.   inline Type& top ();                // Return top item on stack
  90.  
  91.   inline Type& operator[] (unsigned long);    // Zero-relative (top) index 
  92.   long position (const Type&) const;        // Returns stack index of value
  93.   Boolean find (const Type&);            // Returns TRUE if found
  94.  
  95.   CoolStack<Type>& operator= (const CoolStack<Type>& s); // Assignment s = s2;
  96.   Boolean operator== (const CoolStack<Type>& s) const; // is equal
  97.   inline Boolean operator!= (const CoolStack<Type>& s) const ; // is not equal
  98.  
  99.   void resize (long);                // Resize for at least count
  100.   inline long set_length (long);        // Set number of elements
  101.   inline void set_growth_ratio (float);        // Set growth percentage
  102.   void set_compare(Compare = NULL); // Set compare function
  103.   inline void set_alloc_size (int);         // Set alloc size
  104.  
  105.   friend ostream& operator<< (ostream&, const CoolStack<Type>&);
  106.   /*inline##*/ friend ostream& operator<< (ostream&, const CoolStack<Type>*);
  107.  
  108. protected:
  109.   Type* data;                    // Pointer to allocated storage
  110.   static Compare compare_s;    // Pointer operator== function
  111.   Boolean grow (long min_size);            // Grow on push
  112.  
  113. private:
  114.   friend Boolean CoolStack_is_data_equal (const Type&, const Type&);
  115. };
  116.  
  117. // Type& top() -- Return the top item on this stack
  118. // Input:         None
  119. // Output:        Reference to top item on stack
  120.  
  121. template<class Type>
  122. inline Type& CoolStack<Type>::top () {
  123. #if ERROR_CHECKING
  124.   if (this->number_elements > this->size)    // If index out of range
  125.     this->top_error (#Type);            // Raise exception
  126. #endif
  127.   return this->data[this->number_elements-1];    // The top is really at the end
  128. }
  129.  
  130.  
  131. // Type& pop () -- Pop top item off this stack and return it
  132. // Input:          None
  133. // Output:         Top item of stack
  134.  
  135. template<class Type>
  136. inline Type& CoolStack<Type>::pop () {
  137. #if ERROR_CHECKING
  138.   if (this->number_elements > this->size)    // If index out of range
  139.     this->pop_error (#Type);            // Raise expception
  140. #endif
  141.   return (this->data[--this->number_elements]);    // Remove/return top element
  142. }
  143.  
  144.  
  145. // Type& operator[](long) -- Return the nth element (zero-relative from top)
  146. // Input:                    Integer n
  147. // Output:                   Reference to the nth element of stack
  148.  
  149. template<class Type>
  150. inline Type& CoolStack<Type>::operator[] (unsigned long n) {
  151. #if ERROR_CHECKING
  152.   if (unsigned(n) >= this->number_elements)    // If index out of range
  153.     this->bracket_error (#Type, n);        // Raise exception
  154. #endif
  155.   return this->data[number_elements-n-1];    // Nth element from "top"
  156. }
  157.  
  158.  
  159. // Boolean operator!= (CoolStack<Type>&) -- Compare this stack with another stack;
  160. //                                      return TRUE if they are not equal
  161. // Input:  Reference to a stack
  162. // Output: TRUE or FALSE
  163.  
  164. template<class Type>
  165. inline Boolean CoolStack<Type>::operator!= (const CoolStack<Type>& s) const {
  166.   return (!this->operator== (s));
  167. }
  168.  
  169.  
  170. // long set_length(long) -- Change number of elements in this stack
  171. // Input:                   Integer number of elements
  172. // Output:                  None
  173.  
  174. template<class Type>
  175. inline long CoolStack<Type>::set_length (long n) {
  176.   this->CoolBase_Stack::set_length (n, "Type");        // Pass size/type to base class
  177.   return this->number_elements;            // Return value
  178. }
  179.  
  180.  
  181. // void set_growth_ratio(float) -- Set growth percentage of this stack
  182. // Input:                          Float ratio
  183. // Output:                         None
  184.  
  185. template<class Type>
  186. inline void CoolStack<Type>::set_growth_ratio (float ratio) {
  187.   this->CoolBase_Stack::set_growth_ratio (ratio, "Type");    // Pass ratio/type to base
  188.  
  189.  
  190. // void set_alloc_size(int) -- Set the default allocation size
  191. // Input:                      Integer size
  192. // Output:                     None
  193.  
  194. template<class Type>
  195. inline void CoolStack<Type>::set_alloc_size (int size) {
  196.   this->CoolBase_Stack::set_alloc_size (size, "Type");
  197. }
  198.  
  199. // operator<< -- Overload the output operator for CoolStack
  200. // Input:        ostream reference, stack pointer
  201. // Output:       CoolStack data is output to ostream
  202.  
  203. template<class Type>
  204. inline ostream& operator<< (ostream& os, const CoolStack<Type>* s) {
  205.   return operator<<(os, *s);
  206. }
  207.  
  208. #endif                // End #ifdef of STACKH
  209.  
  210.